﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using CommonClass;

namespace SurveillanceLib
{
    /// <summary>
    /// 用于预览界面的分屏
    /// </summary>
    public class PanelOperationExt
    {
        #region 属性

        /// <summary>
        /// 记录所选 Panel，只能选择一个 Panel【Static】
        /// </summary>
        public static Panel SelectedPanel { get; private set; }

        /// <summary>
        /// 是否隐藏外边框
        /// </summary>
        public bool IsHideBorder { get; set; }

        /// <summary>
        /// 是否屏蔽点击事件
        /// </summary>
        public bool IsHideClickEvent { get; set; }

        #endregion

        #region 方法

        /// <summary>
        /// 将 SelectedPanel 设为 null
        /// </summary>
        public void SetNull2SelectedPanel()
        {
            SelectedPanel = null;
        }

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="screenCount">分屏个数</param>
        /// <param name="splitScreen">基分屏</param>
        /// <param name="doubleAction">双击事件</param>
        /// <param name="panelInfor">相关列表，主要用于修改新创建的小分屏，一般情况下该列表的个数与分屏个数应保持一致</param>
        /// <param name="isShowLabelAndColor">是否在 Panel 上显示标签与背景色</param>
        /// <param name="toolTip">ToolTip 实例</param>
        /// <param name="mouseEnterAction">鼠标进入事件</param>
        /// <returns>所有分割的窗口</returns>
        public List<Panel> SplitScreens(int screenCount, Panel splitScreen,
            Action doubleAction = null, Action mouseEnterAction = null,
            List<Panel> panelInfor = null, bool isShowLabelAndColor = false, ToolTip toolTip = null)
        {
            try
            {
                SplitScreen splitscreen = new SplitScreen(); // 画面分割
                List<Panel> panelList = new List<Panel>(); // 保存分屏列表
                IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(screenCount, splitScreen.Size);

                splitScreen.Controls.Clear(); // 先清除 Panel 已存在的内容
                for (int i = 0; i < rectList.Count; i++)
                {
                    Panel panel = new Panel()
                    {
                        BackColor = Color.Black,
                        Width = rectList[i].Width,
                        Height = rectList[i].Height,
                        Left = rectList[i].X,
                        Top = rectList[i].Y,
                        Padding = new Padding(0),
                        Margin = new Padding(0),
                        Tag = new TagModel()
                        {
                            Panel_Index = i,
                        },
                    };

                    panel.MouseEnter += (s, e) =>
                    {
                        if (mouseEnterAction != null)
                        {
                            mouseEnterAction();
                        }
                    };

                    // 处理 panel 的相关信息
                    if (panelInfor != null && i < panelInfor.Count && panelInfor[i] != null &&
                        panelInfor[i].Tag != null && panelInfor[i].Tag is TagModel &&
                        !string.IsNullOrWhiteSpace((panelInfor[i].Tag as TagModel).Panel_SelectedChannel)) // 所选通道不为空时
                    {
                        if ((panelInfor[i].Tag as TagModel).Driver_IsLogin) // 设备已登录
                            SurveillanceUtilities.SetPanelTooltip(panel, panelInfor[i].Tag as TagModel, toolTip, isShowLabelAndColor, Color.LimeGreen); // 设置 Tooltip
                        else
                            SurveillanceUtilities.SetPanelTooltip(panel, panelInfor[i].Tag as TagModel, toolTip, isShowLabelAndColor, Color.Red, "设备登录失败"); // 设置 Tooltip
                    }

                    // 定义事件
                    if (!IsHideClickEvent)
                    {
                        panel.Click += (s, e) =>
                        {
                            SelectedPanel = s as Panel;
                            splitScreen.Refresh(); // 重刷界面：将边框颜色重置

                            if (!IsHideBorder)
                            {
                                Utilities.PaintPanelBorder(s as Panel, Color.LightGreen, 2, ButtonBorderStyle.Solid); // 绘制边框
                            }
                        };

                        panel.DoubleClick += (s, e) =>
                        {
                            SelectedPanel = s as Panel;
                            if (doubleAction != null)
                                doubleAction();
                        };
                    }

                    splitScreen.Controls.Add(panel);
                    panelList.Add(panel);
                }

                return panelList;
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return null;
            }
        }

        #endregion
    }
}
